package me.sdau.spider.openapi.exception;

import lombok.extern.slf4j.Slf4j;
import me.sdau.spider.openapi.dto.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = AuthException.class)
    public ResponseEntity<Result> authError(AuthException e) {
        log.warn("error ==> [{}]",e.getMessage());
        return new ResponseEntity<>(Result.of(e.getMessage()), HttpStatus.UNAUTHORIZED);
    }

    @ExceptionHandler(value = QueryParamException.class)
    public ResponseEntity<Result> authError(QueryParamException e) {
        log.warn("error ==> [{}]",e.getMessage());
        return new ResponseEntity<>(Result.of("参数错误"), HttpStatus.BAD_REQUEST);
    }


    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<Result> error(Exception e) {
        log.error("服务器错误 ==> [{}]", e.getMessage(), e);
        return new ResponseEntity<>(Result.of("服务器错误，请稍后重试！"), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
